﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SynoManager.DownloadStation
{
    public class DownloadTaskFileCollection : IEnumerable<DownloadTaskFile>
    {
        private readonly DownloadTask task;
        private DownloadTaskFile[] files;

        internal DownloadTaskFileCollection(DownloadTask task)
        {
            this.task = task;
            Reload();
        }

        public void Reload()
        {
            var result = task.service.client.GetFileList(task.TaskID);
            files = new DownloadTaskFile[result.total];
            for (int i = 0; i < files.Length; i++)
                files[i] = new DownloadTaskFile(result.items[i]);
        }

        public void Save()
        {
            var list = new List<DownloadStationClient.ApplyTaskFileInfo>();
            foreach (DownloadTaskFile file in files)
            {
                if (file.Current != file.Download)
                {
                    list.Add(new DownloadStationClient.ApplyTaskFileInfo() {name = file.Name, dl = file.Download});
                    file.Current = file.Download;
                }
            }
            if (list.Count > 0)
                task.service.client.ApplyTask(task.TaskID, list.ToArray(), task.SeedingInterval, task.SeedingRatio);
        }

        public int Count
        {
            get { return files.Length; }
        }

        public DownloadTaskFile this[int index]
        {
            get { return files[index]; }
        }

        #region IEnumerable<DownloadTaskFile> Members

        public IEnumerator<DownloadTaskFile> GetEnumerator()
        {
            foreach (DownloadTaskFile file in files)
                yield return file;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return files.GetEnumerator();
        }

        #endregion
    }

    public class DownloadTaskFile
    {
        private string name;
        private long size;
        private bool download;
        private bool newvalue;

        public DownloadTaskFile(DownloadStationClient.DownloadTaskFileInfo fileInfo)
        {
            this.name = fileInfo.name;
            this.size = ByteSize.Parse(fileInfo.size);
            this.download = newvalue = fileInfo.dl;
        }

        public string Name
        {
            get { return name; }
        }

        public ByteSize Size
        {
            get { return new ByteSize(size); }
        }

        public bool Download
        {
            get { return newvalue; }
            set { newvalue = value; }
        }

        public bool Current
        {
            get { return download; }
            internal set { download = value;}
        }

        public override string ToString()
        {
            return String.Format("{0} ({1})", Name, Size);
        }
    }
}
